import {
    HTTP
} from '../../../../utils/http.js'
let ajax = new HTTP()
var app = getApp()
Page({
    data: {
        imgUrls: [],
        bannerpic: [],
        applyId: "",
        nameValue: "",
        phoneValue: "",
        applyTitle: "",
        totalIndex: 0,
        markIndex: 0,
        totalList: [],
        markList: [],
        agreeFlag: false,
        agreeYes: '../images/002-active.png',
        agreeNo: '../images/002.png',
        isPay: 0,
        pid: '',
        zongId: "",
        fenId: "",
        prize: 0
    },
    onLoad(options) {
        this.data.applyId = options.id
        this.data.applyTitle = options.title
        wx.setNavigationBarTitle({
            title: this.data.applyTitle
        })
        this.btnselect(app.globalData.memberid, this.data.applyId)
        this.getBannerInfo()
    },
    // 转发 
    onShareAppMessage: function () {

    },
    // 确认报名
    sureApply() {
        let aid = this.data.applyId
        let mid = app.globalData.memberid
        let name = this.data.nameValue
        let phone = this.data.phoneValue
        let totalValue = this.data.zongId
        let markValue = this.data.fenId
        if (name === "" || phone === "") {
            wx.showToast({
                title: '信息不能为空',
                icon: 'none'
            })
            return
        }
        if (!this.data.agreeFlag) {
            wx.showToast({
                title: '同意协议才能报名',
                icon: 'none'
            })
            return
        }
        console.log(111)
        // 数据提交
        ajax.request({
            url: "sportslist",
            method: "POST",
            data: {
                memberid: mid,
                sportid: aid,
                itemid: markValue,
                name: name,
                phone: phone
            },
            success: (res) => {
                if (res.rescode === "0000") {
                    wx.showToast({
                        title: '报名成功',
                        icon: 'success',
                        duration: 1500
                    })
                    setTimeout(function () {
                        wx.switchTab({
                            url: '/pages/find/find',
                        })
                    }, 1500)
                }
            }
        })
    },
    //拿到总项
    btnselect(memberid, applyId) {
        ajax.request({
            url: "sportsitem",
            data: {
                memberid: memberid,
                sportsid: applyId
            },
            success: (res) => {
                this.setData({
                    totalList: res.data,
                    zongId: res.data[0].id
                })
                this.btnselectfen(memberid, applyId, this.data.zongId)
            }
        })
    },
    // 总类改变
    bindTotalPickerChange(e) {
        this.setData({
            totalIndex: e.detail.value,
            markIndex: 0
        })
        let id = this.data.totalList[this.data.totalIndex].id
        this.setData({
            zongId: id
        })
        this.btnselectfen(app.globalData.memberid, this.data.applyId, id)
    },
    // 选择分项
    btnselectfen(memberid, applyId, pid) {
        ajax.request({
            url: "sportsitem",
            data: {
                memberid: memberid,
                pid: pid,
                sportsid: applyId
            },
            success: (res) => {
                this.setData({
                    markList: res.data,
                    fenId: res.data[0].id
                })
            }
        })
    },
    // 分类改变
    bindMarkPickerChange(e) {
        this.setData({
            markIndex: e.detail.value
        })
        let id = this.data.markList[this.data.markIndex].id
        this.setData({
            fenId: id
        })
    },
    // 获取banner
    getBannerInfo() {
        let img = wx.getStorageSync('pic')
        let title = wx.getStorageSync('title')
        let prize = wx.getStorageSync('prize')
        let isPay = wx.getStorageSync('isPay')
        let imgArr = []
        imgArr.push({
            bannerpic: img,
            name: title
        })
        this.setData({
            imgUrls: imgArr,
            prize: prize,
            isPay: isPay
        })
    },
    // 同意条例
    agreeApply() {
        if (this.data.agreeFlag) {
            this.setData({
                agreeFlag: false
            })
        } else {
            this.setData({
                agreeFlag: true
            })
        }
    },
    // 姓名失焦
    inputNameBlur(event) {
        this.setData({
            nameValue: event.detail.value
        })
    },
    // 手机号失焦
    inputPhoneBlur(event) {
        let phone = event.detail.value
        if (!this._isPhoneNumber(phone)) {
            wx.showToast({
                title: '手机号码有误',
                icon: 'none'
            })
            this.setData({
                phoneValue: ""
            })
            return
        } else {
            this.setData({
                phoneValue: phone
            })
        }
    },
    // 手机号判断
    _isPhoneNumber(phone) {
        if (!(/^1[34578]\d{9}$/.test(phone)) || phone == "") {
            return false;
        } else {
            return true;
        }
    }
})